/**
  ******************************************************************************
  * @file    main.c 
  * @author  MCD Application Team
  * @version V1.0.0
  * @date    18-April-2011
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************  
  */ 

/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx.h"
#include "usart.h"
#include "camera/dcmi_OV2640.h"

/** @addtogroup STM32F2xx_StdPeriph_Examples
  * @{
  */

/** @addtogroup IOToggle
  * @{
  */ 
void LCD_CS_ResetBits(void);
void Delay(__IO uint32_t nTime);
void TimingDelay_Decrement(void);
static __IO uint32_t TimingDelay;
uint32_t JpegDataCnt=0;
extern uint8_t JpegBuffer[1024*33];
uint8_t jpg_flag=0;
uint8_t key_flag=0;

/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{	
	OV2640_IDTypeDef OV2640ID;
	RCC_ClocksTypeDef SYS_Clocks;
	uint32_t i=0;

	if (SysTick_Config(SystemCoreClock / 1000))
  	{ 
	    /* Capture error */ 
	    while (1);
  	}
	RCC_GetClocksFreq(&SYS_Clocks);

	USART_Configuration();

	printf("\r\nSYSCLK:%dM\r\n",SYS_Clocks.SYSCLK_Frequency/1000000);
	printf("HCLK:%dM\r\n",SYS_Clocks.HCLK_Frequency/1000000);
	printf("PCLK1:%dM\r\n",SYS_Clocks.PCLK1_Frequency/1000000);
	printf("PCLK2:%dM\r\n",SYS_Clocks.PCLK2_Frequency/1000000);


		OV2640_Init();
		
	if(DCMI_OV2640_ReadID(&OV2640ID)==0)
	{	
		if(OV2640ID.Manufacturer_ID1==0x7f && OV2640ID.Manufacturer_ID2==0xa2 
			&& OV2640ID.Version==0x26 && OV2640ID.PID==0x42){
 			printf("OV2640 ID:0x%x 0x%x 0x%x 0x%x\r\n",
 				OV2640ID.Manufacturer_ID1, OV2640ID.Manufacturer_ID2, OV2640ID.PID, OV2640ID.Version);
		}
		else{
 			printf("OV2640 ID is Error!\r\n");
		}			
	}
		
		OV2640_JPEGConfig(JPEG_320x240);
		OV2640_BrightnessConfig(0x20);	
		OV2640_AutoExposure(2);	
	
 		EXTI_Config();
	
		Delay(10);		
	
		printf("\r\n Please press the button to take photos \r\n");
		
	while (1)
	{
		if(jpg_flag&&key_flag)
		{
			DCMI_Cmd(DISABLE); 
			DCMI_CaptureCmd(DISABLE);
			DMA_Cmd(DMA2_Stream1, DISABLE);
			if( (JpegBuffer[0]==0xFF)&&(JpegBuffer[1]==0xD8) )
			{
					while( !( (JpegBuffer[JpegDataCnt-2]==0xFF) && (JpegBuffer[JpegDataCnt-1]==0xD9) ) ) 
				{		
					JpegDataCnt++;
				}				
				 for(i = 0; i < JpegDataCnt; i++)	//sizeof(JpegBuffer)
				{
					USART3_Transmit(JpegBuffer[i]);
				} 
			}
			JpegDataCnt = 0;
			jpg_flag = 0;
			key_flag = 0;	
	
		}
  }
}

void Delay(__IO uint32_t nTime)
{ 
  TimingDelay = nTime;

  while(TimingDelay != 0)
  {}
}

void TimingDelay_Decrement(void)
{
  if (TimingDelay != 0x00)
  { 
    TimingDelay--;
  }
}

#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {

  }
}
#endif

/**
  * @}
  */ 

/**
  * @}
  */ 

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
